<template>
  <el-dialog title="文章类型新增" :close-on-click-modal="false" :visible.sync="visible" center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="文章类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>

  </el-dialog>
</template>

<script>
  import {
    getWxArticleTypeObj,
    addWxArticleTypeObj,
    putWxArticleTypeObj
  } from '@/api/mp/wxarticletype'
  export default {
    data(){
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id:'',
          typeName: ''
        },
        dataRule: {
          typeName: [{required: true,message: '文章类型名称不能为空',trigger: 'blur'}]
        }
      }
    },
    methods: {
      init(id){
        this.visible = true
        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxArticleTypeObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putWxArticleTypeObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addWxArticleTypeObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>

<style>
  table {
    width: 100%;
  }

  tr td {
    border: solid 1px #e7e7e7;
  }
</style>
